import {
  registerUser,
  loginUser,
  addUserRequest,
  getAllUsersRequest,
  editUsersRequest,
  deteleUserstRequest,
  editUsersPowerRequest,
  getUserPower
} from '@/service/user/user.js'
import { ElMessage } from 'element-plus'
import router from '@/router'
import cache from '@/util/cache.js'
import { getRolesNavRequest } from '@/service/nav/nav.js'
import { mapMenu } from '@/util/map-menu.js'
export default {
  state: {
    userInfo: {},
    userList: [],
    userNav: []
  },
  mutations: {
    changeUser(state, user) {
      state.userInfo = user
    },
    changeUserList(state, userList) {
      state.userList = userList
    },
    changeUserNav(state, userNav) {
      state.userNav = userNav
      //动态添加路由
      mapMenu(userNav).forEach((route) => {
        router.addRoute(route)
      })
    }
  },
  actions: {
    async requestUserNav({ commit }) {
      const uId = cache.getCache('jxcms-user').uId
      const res = await getUserPower({ uId })
      let uPower = []
      if (res.uPower) {
        uPower = res.uPower.split(',')
      }
      const rPower = res.rPower.split(',')
      let powerArr = []
      if (res.uPower) {
        powerArr = [...uPower, ...rPower]
      } else {
        powerArr = [...rPower]
      }
      const navList = await getRolesNavRequest({
        roleList: [...new Set(powerArr)].join(',')
      })
      commit('changeUserNav', navList)
      cache.setCache('jxcms-userNav', navList)
    },
    async registerUser({ commit }, info) {
      const registerres = await registerUser(info)
      if (registerres === 1) {
        ElMessage({
          message: '注册成功!',
          type: 'success'
        })
        router.push('/login')
      } else {
        ElMessage({
          message: '注册失败',
          type: 'error'
        })
      }
    },
    async requestAllUser({ commit }, page) {
      const allUser = await getAllUsersRequest(page)
      commit('changeUserList', allUser)
    },
    async EditRequestUser({ dispatch, commit }, data) {
      await editUsersRequest(data)
      dispatch('requestAllUser', 1)
    },
    async EditRequestUserPower({ dispatch, commit }, data) {
      await editUsersPowerRequest(data)
      dispatch('requestAllUser', 1)
    },
    async deteleRequestUsers({ dispatch, commit }, uId) {
      await deteleUserstRequest({
        uId
      })
      dispatch('requestAllUser', 1)
    },
    async addUser({ commit }, info) {
      await addUserRequest(info)
    },
    async loginUser({ dispatch, commit }, info) {
      const loginRes = await loginUser(info)
      if (loginRes.uName) {
        if (!cache.getCache('jxcms-token')) {
          ElMessage({
            message: '登录成功!',
            type: 'success'
          })
        }
        commit('changeUser', loginRes)
        //本地存储
        cache.setCache('jxcms-user', loginRes)
        dispatch('requestUserNav')
        if (!cache.getCache('jxcms-token')) {
          router.push('/roles')
        }
        cache.setCache('jxcms-token', loginRes.token)
      } else {
        ElMessage({
          message: '登录失败',
          type: 'error'
        })
      }
    }
  }
}
